package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootClub;
import org.footballmanager.pandafootball.system.form.impl.FootClubSearchForm;
import org.footballmanager.pandafootball.system.service.FootClubService;
import org.footballmanager.pandafootball.system.service.FootQdService;
import org.footballmanager.pandafootball.system.service.FootSjService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@SessionAttributes("footClubSearchForm")
@RequestMapping("/foot/footClub")
public class FootClubController extends AddAndEditController<FootClub> {
    /* 实体名称 */
    private static final String ENTITYNAME = "footClub";
    private static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.FOOT, ENTITYNAME);
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
    private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

    @Autowired
    private FootClubService footClubService;
    @Autowired
    private FootUserService footUserService;
    @Autowired
    private FootSjService footSjService;
    @Autowired
    private FootQdService footQdService;

    /**
     * 抽象方法
     *
     * @return 实体名称
     */
    @Override
    protected String getEntityName() {
        return ENTITYNAME;
    }

    /**
     * 初始化
     *
     * @param map                参数
     * @param footClubSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootClubSearchForm footClubSearchForm) {
        map.addAttribute("userList", footUserService.findGameIdIsNotNull());
        map.addAttribute("sjList", footSjService.getall());
        map.addAttribute("qdList", footQdService.getall());
        FootClubSearchForm newFootClubSearchForm = new FootClubSearchForm();
        if (footClubSearchForm != null) {
            newFootClubSearchForm = footClubSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootClubSearchForm);
    }

    /**
     * 俱乐部列表
     *
     * @param map                参数
     * @param footClubSearchForm 条件参数
     * @return 页面
     */
    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute FootClubSearchForm footClubSearchForm) {
        footClubService.search(ENTITYNAME, map, footClubSearchForm);
        return LISTPAGE;
    }

    /**
     * 打开修改俱乐部页面
     *
     * @param map 参数
     * @param id  主键
     * @return 页面
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String openEditPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, footClubService.get(id));
        return EDITPAGE;
    }

    /**
     * 打开修改俱乐部队壳页面
     *
     * @param map 参数
     * @param id  主键
     * @return 页面
     */
    @RequestMapping(value = "/dwedit", method = RequestMethod.GET)
    public String openDwEditPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, footClubService.get(id));
        return "foot/footClub/DwEdit";
    }

    /**
     * 修改俱乐部队壳
     *
     * @param footClub      俱乐部实体类
     * @param bindingResult 校验
     * @param request       请求
     * @param s             session状态
     * @return 页面
     */
    @RequestMapping(value = "/dwedit", method = RequestMethod.POST)
    public String dwUpdate(@Valid @ModelAttribute FootClub footClub, BindingResult bindingResult,
                           HttpServletRequest request, SessionStatus s) {
        if (!bindingResult.hasErrors()) {
            footClubService.changeQd(footClub, request);
            s.setComplete();
            return REDIRECT;
        } else {
            return "foot/footClub/DwEdit";
        }
    }

    /**
     * 浏览俱乐部记录
     *
     * @param map                参数
     * @param id                 主键
     * @param footClubSearchForm 条件参数
     * @return 页面
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootClubSearchForm footClubSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footClubSearchForm);
        map.addAttribute(ENTITYNAME, footClubService.get(id));
        return VIEWPAGE;
    }

}
